setwd("C:/Documents and Settings/ORIE_user1/My Documents/Distance Covariance Project/R Code/")
#setwd("C:/Documents and Settings/1083117/My Documents/Distance Covariance Project/R Code")
#optional to get the analysis running...
source("Functions/source.all.R")
source.all() #this will produce errors if there are any uninstalled packages
install.all.packages() #install any missing packages
source.all() #source again...

##############################################################################
###############################BEGIN ANALYSIS#################################
##############################################################################


##simulate arma-garch (p,q,r,s) models, estimate their parameters, and 
model.index = 1
statistics.select = 1
ns=c(200)
N=10
alpha.levels = c(0.01,0.05,0.1)
modelnames = NULL
models = get.models(model.index)$models
for(i in 1:length(ns)){
	statistics.matrix = NULL
	for (j in 1:N){ 	
            simvalues = NULL
		statistics = NULL
		for(k in 1:length(models)){
			#get simulation runs for this value of n
			simvalues.temp = auto.sim(models[k],ns[i])$sim.values
			#compute statistics
			statistics.temp = compute.statistics(simvalues.temp,statistics.select)
			#aggregate
			simvalues = cbind(simvalues,simvalues.temp)		
			statistics = cbind(statistics,statistics.temp)
		}	
			if(j==1){
				column.names = as.array(paste(models,";",as.character(ns[i]),sep=""))
				simvalues = as.data.frame(simvalues)
				names(simvalues)= column.names				
				dimnames(statistics)[[2]]=column.names				
				statistics.matrix = statistics
			}else{				
				statistics.matrix=rbind(statistics.matrix,statistics)				
			}
				
	}
      
}
	
	empirical.power.values = compute.empirical.powers(statistics.matrix,alpha.levels)



arma.garch.sim(c(.9),NULL,NULL,NULL,NULL,NULL,NULL,n=200,NULL, "norm")
model = do.vol.modeling(test,test.data.vector,phase1fits=c(0),x=test.data.vector,dates=c(0),automatic=T,plot=F,useGPH=F)

#before model
dcor=auto.dcor(x,x,12)
plot(dcor$lag,dcor$adcor,type='h',col='black')
points(dcor$lag,dcor$p.val,col='red')
abline(b=0,a=alpha,col='blue',lty=2)

#after model
res=model$res
dcor=auto.dcor(res,res,12)
plot(dcor$lag,dcor$adcor,type='h',col='black')
points(dcor$lag,dcor$p.val,col='red')
abline(b=0,a=alpha,col='blue',lty=2)
model$model.check